<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排序</title>
</head>
<script>
    var arr=[{
        name:'a',
        age:10,
        sarlary:1000
    },{
        name:'f',
        age:80,
        sarlary:600
    },{
        name:'b',
        age:23,
        sarlary:1500
    },{
        name:'c',
        age:54,
        sarlary:800
    }]  
    console.log(arr,'===')
    function myCompare(propertyName) {
        return function(obj1, obj2) {
            // console.log(obj1)
            var val1 = obj1[propertyName];
            var val2 = obj2[propertyName];
            if (val1 > val2) {
                return 1;
            } else if (val1 < val2) {
                return -1;
            }
        }
    }
    arr.sort(myCompare("age"));
    console.log(arr);
      
</script>
<body>
    
</body>
</html>